var queryYear;
// 社会评价人员类型
var DemocracyCheckman = Ext.data.Record.create([{
	name : 'id',
	mapping : 'id',
	type : 'int'
}, {
	name : 'name',
	mapping : 'name',
	type : 'string'
}, {
	name : 'ordering',
	mapping : 'ordering',
	type : 'float'
}, {
	name : 'weight',
	mapping : 'weight',
	type : 'float'
}, {
	name : 'year',
	mapping : 'year',
	type : 'int'
}]);

var ds_democracycheckman_select = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/democracycheckman/findAllDemocracyCheckman.action'
	}),
//	autoLoad: true,
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
		name : 'id',
		type : 'int'
	}, {
		name : 'name',
		type : 'string'
	},{
		name : 'ordering',
		type : 'string'
	},{
		name : 'weight',
		type : 'string'
	},{
		name : 'year',
		type : 'int'
	}])
});

var sm = new Ext.grid.RowNumberer();
var cm_democracycheckman = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'name',header: "责任人名称", width: 300, sortable: true, dataIndex: 'name'},
    {id: 'ordering',header: "排序", width: 60, hidden: true, dataIndex: 'ordering'},
    {id: 'weight',header: "评分系数", width: 60, sortable: true, dataIndex: 'weight'},
    {id: 'year',header: "考核年度", width: 120, dataIndex: 'year'}
]);

cm_democracycheckman.defaultSortable = false;
var window_add_democracycheckman = new Ext.Window({
			title : '添加社会评价指标评议',
			width : 480,
			height : 640,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('添加社会评价人员类型');
					Ext.getCmp("form_add_democracycheckman").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_add_democracycheckman',
						labelWidth : 100,
						labelAlign : 'right',
						url : '/supervise/biz/democracycheckman/saveDemocracyCheckman.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						defaults : {
							anchor : '90%',
							width : 360,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
									xtype : 'hidden',
									name : 'democracyCheckman.id'
								}, {
									fieldLabel : '<span style="color: red">*&nbsp;</span>责任人名称',
									name : 'democracyCheckman.name',
									xtype : 'textarea',
									allowBlank : false,
									maxLength : 200
								}, {
									xtype : 'hidden',
									name : 'democracyCheckman.ordering'
								}, {

									xtype : 'numberfield',
									fieldLabel : '<span style="color: red">*&nbsp;</span>评分系数',
									name : 'democracyCheckman.weight',
									allowBlank : false
								}, {
									xtype : 'combo',
									fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
									hiddenName : 'democracyCheckman.year',
									mode : 'local',
									store : store_year,
									valueField : 'value',
									displayField : 'text',
									value: check_year,
									selectOnFocus : true,
									editable : false,
									allowBlank : false,
									readOnly : true,
									triggerAction : 'all',
									emptyText : '请选择考核年度'
								}],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '添加',
									handler : function(btn) {
										var frm = Ext.getCmp("form_add_democracycheckman").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('democracyCheckman.name');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var store = grid_democracycheckman.getStore();
															var democracycheckman = new DemocracyCheckman({
																		id : action.result.id,
																		name : cnfield.getValue(),
																		ordering : form.findField('democracyCheckman.ordering').getValue(),
																		weight : form.findField('democracyCheckman.weight').getValue(),
																		year : form.findField('democracyCheckman.year').getValue()
																	});
															store.insert(0, [democracycheckman]);
															grid_democracycheckman.getView().refresh();
															btn.enable();
															form.reset();
															window_add_democracycheckman.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '重置',
									handler : function() {
										Ext.getCmp("form_add_democracycheckman").getForm().reset();
									}
								}, {
									text : '取消',
									handler : function() {
										window_add_democracycheckman.hide();
									}
								}]
					})]
		});
		
var window_edit_democracycheckman = new Ext.Window({
			title : '修改社会评价人员类型',
			width : 480,
			height : 640,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('修改社会评价人员类型');
					Ext.getCmp("form_edit_democracycheckman").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_edit_democracycheckman',
						labelWidth : 100,
						labelAlign : 'right',
						url : '/supervise/biz/democracycheckman/saveDemocracyCheckman.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						defaults : {
							anchor : '90%',
							width : 360,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
									xtype : 'hidden',
									name : 'democracyCheckman.id'
								}, {
									fieldLabel : '<span style="color: red">*&nbsp;</span>责任人名称',
									name : 'democracyCheckman.name',
									xtype : 'textarea',
									allowBlank : false,
									maxLength : 200
								}, {
									xtype : 'hidden',
									name : 'democracyCheckman.ordering'
								}, {

									xtype : 'numberfield',
									fieldLabel : '<span style="color: red">*&nbsp;</span>评分系数',
									name : 'democracyCheckman.weight',
									allowBlank : false
								}, {
									xtype : 'combo',
									fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
									hiddenName : 'democracyCheckman.year',
									mode : 'local',
									store : store_year,
									valueField : 'value',
									displayField : 'text',
									value: check_year,
									selectOnFocus : true,
									editable : false,
									allowBlank : false,
									readOnly : true,
									triggerAction : 'all',
									emptyText : '请选择考核年度'
								}],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '修改',
									handler : function(btn) {
										var frm = Ext.getCmp("form_edit_democracycheckman").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('democracyCheckman.name');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var record = grid_democracycheckman.getSelectionModel().getSelected();
															// set value
															record.set("name", frm.findField('democracyCheckman.name').getValue());
															record.set("ordering", frm.findField('democracyCheckman.ordering').getValue());
															record.set("weight", frm.findField('democracyCheckman.weight').getValue());
															record.set("year", frm.findField('democracyCheckman.year').getValue());
															record.commit();
															frm.reset();
															btn.enable();
															window_edit_democracycheckman.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '取消',
									handler : function() {
										window_edit_democracycheckman.hide();
									}
								}]
					})]
		});
var btn_add_democracycheckman = new Ext.Button({
			text : '添加',
			iconCls : 'icon-add',
			handler : function() {
				window_add_democracycheckman.show();
			}
		});
var btn_edit_democracycheckman = new Ext.Button({
			text : '修改',
			iconCls : 'icon-edit',
			handler : function() {
				if (!grid_democracycheckman.checkOne()) {
					return;
				}
				window_edit_democracycheckman.show(this);
				var record = grid_democracycheckman.getSelectionModel().getSelected();
				var frm = Ext.getCmp("form_edit_democracycheckman").getForm();
				// set value
				frm.findField('democracyCheckman.id').setValue(record.get("id"));
				frm.findField('democracyCheckman.name').setValue(record.get("name"));
				frm.findField('democracyCheckman.ordering').setValue(record.get("ordering"));
				frm.findField('democracyCheckman.weight').setValue(record.get("weight"));
				frm.findField('democracyCheckman.year').setValue(record.get("year"));
			}
		});
var btn_del_democracycheckman = new Ext.Button({
			text : '删除',
			iconCls : 'icon-del',
			handler : function() {
				var record = grid_democracycheckman.getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
												url : '/supervise/biz/democracycheckman/deleteDemocracyCheckman.action',
												params : {
													id : record.data.id
												},
												success : function() {
													grid_democracycheckman.getStore().remove(record);
													grid_democracycheckman.getView().refresh();
												},
												failure : function() {
													Ext.Msg.show({
																title : '错误提示',
																msg : '删除时发生错误!',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
				}
			}
		});
var btn_search_democracycheckman = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : function() {
		ds_democracycheckman.load({params: {year: queryYear}});
	}
});

var ds_democracycheckman = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/democracycheckman/findDemocracyCheckmanByPage.action'
	}),
//	autoLoad: true,
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
		name : 'id',
		type : 'int'
	}, {
		name : 'name',
		type : 'string'
	},{
		name : 'ordering',
		type : 'string'
	},{
		name : 'weight',
		type : 'string'
	},{
		name : 'year',
		mapping : 'year',
		type : 'int'
	}])
});

var grid_democracycheckman = new Ext.grid.GridPanel({
			title : '社会评价人员类型',
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit : true
			},
			region : 'center',
			cm : cm_democracycheckman,
			sm : new Ext.grid.RowSelectionModel({
						singleSelect : true
					}),
			store : ds_democracycheckman,
			autoExpandColumn : 'name',
			buttons: [btn_search_democracycheckman],
			tbar : [btn_add_democracycheckman, '-', btn_edit_democracycheckman, '-', btn_del_democracycheckman, '-',
					{xtype: 'tbtext', text: '考核年度: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '},
					new Ext.form.ComboBox({
						xtype : 'combo',
						fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
						width: 120,
						hiddenName : 'checkYear',
						valueField : 'value',
						displayField : 'text',
						mode : 'local',
						store : ds_year_select,
						value : new Date().getFullYear(),
						selectOnFocus : true,
						editable : false,
						allowBlank : false,
						width : 80,
						typeAhead : true,
						readOnly : true,
						triggerAction : 'all',
						loadingText : '加载中...',
						emptyText : '请选择考核年度',
						listeners : {
							'select' : function(combo, record, index) {
								queryYear = combo.getValue();
							}
						}
					}),{xtype: 'tbtext', text: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'}, btn_search_democracycheckman],
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_democracycheckman,
						displayInfo : false,
						displayMsg : '第 {0} - {1} 条  共 {2} 条',
						refreshText : "刷新",
						emptyMsg : "没有记录"
					}),
			// 检测至少选择一个
			checkOne : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请选择一条的记录！");
					return false;
				} else if (selections.length != 1) {
					Ext.MessageBox.alert("提示", "不能选择多行！");
					return false;
				}
				return true;
			},

			// 检测必须选择一个
			checkMany : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
					return false;
				}
				return true;
			},
			listeners : {
				'render' : function(e) {
					ds_democracycheckman.load();
				}
			}

		});
//var qbar = new Ext.Toolbar({
//	renderTo : grid_democracycheckman.tbar,
//	items: ['query']
//}
//);
//  var bbar2 = new Ext.Toolbar({
//			renderTo : grid_democracycheckman.tbar,
//			items : ['Example of second toolbar', '-', {
//						text : 'Button',
//						iconCls : 'icon-key'
//					}, '-']
//		}); 
//		grid_democracycheckman.syncSize();
var p_democracycheckman = {
	id : 'democracycheckman-panel',
	border : false,
	layout : 'border',
	items : [grid_democracycheckman]
};